package com.runen.wnhz.runen.ui.activity.mine;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.data.entity.UcenterEntity;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerMineComponent;
import com.runen.wnhz.runen.di.module.MineModule;
import com.runen.wnhz.runen.presenter.Contart.MineContart;
import com.runen.wnhz.runen.presenter.iPresenter.IMinePersenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.adapter.puadapter.BalanceAdapter;
import com.runen.wnhz.runen.widget.TitleBuilder;
import com.runen.wnhz.runen.widget.flowtaglayout.FlowTagLayout;
import com.runen.wnhz.runen.widget.flowtaglayout.OnTagSelectListener;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018-04-12.
 */

public class BalanceActivity extends BaseActivity<IMinePersenter> implements MineContart.View, View.OnClickListener {

    @BindView(R.id.tv_play)
    TextView tv_play;

    @BindView(R.id.tv_user_money)
    TextView tv_user_money;

    @BindView(R.id.et_money)
    EditText et_money;

    @BindView(R.id.fl_select_money)
    FlowTagLayout fl_select_money;

    private UserBean reqeustUser;

    private BalanceAdapter mAdapter;

    private String mRid = "";
    private String mRechargeType = "1";  // 1 - 自定义 2 - 系统定义


    @Override
    protected int getLayoutView() {
        return R.layout.balance_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                break;
            default:
                break;
        }
    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this, R.color.title_green);
        //获取余额
        initOnClick();

        mAdapter = new BalanceAdapter(this);
        fl_select_money.setAdapter(mAdapter);
        fl_select_money.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        fl_select_money.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (!selectedList.isEmpty()) {
                    int position = selectedList.get(0);
                    String money = mAdapter.getData().get(position).getTitle();
                    et_money.setText(money.substring(0, money.length() - 1));
                    mRid = mAdapter.getData().get(position).getRid();
                    mRechargeType = "2";
                } else {
                    mRechargeType = "1";
                }
            }
        });
    }

    @Override
    public void initPresente() {
        super.initPresente();
        mPresenter.requesetUserMone();
        /*获取充值列表*/
        mPresenter.reqeustUserMoneList();
    }

    private void initOnClick() {
        tv_play.setOnClickListener(this);
    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("我的余额")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
        reqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);
    }


    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerMineComponent.builder().applicationComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().initject(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_play:
                if (TextUtils.isEmpty(et_money.getText().toString().trim())) {
                    Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent = new Intent(this, PayActivity.class);
                intent.putExtra("PayType", 1);
                intent.putExtra("RechargeType", mRechargeType);
                intent.putExtra("Rid", mRid);
                intent.putExtra("RechargeMoney", et_money.getText().toString().trim());
                startActivity(intent);
                break;
        }
    }

    @Override
    public String getToken() {
        return reqeustUser.getToken();
    }

    @Override
    public void showMoney(String data) {
        tv_user_money.setText(data);
    }

    @Override
    public void getMoneyList(final List<UcenterEntity> data) {
        mAdapter.onlyAddAll(data);
    }
}
